package com.jxc.summer.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.jxc.summer.dao.ColorMapper;
import com.jxc.summer.service.ColorService;
import com.jxc.summer.util.JacksonUtil;
import com.jxc.summer.util.ReturnT;
import com.jxc.summer.util.UUIDUtil;

@Controller
@RequestMapping("/colorinfo")
public class ColorController {

	@Autowired
	private ColorService colorService;
	@Autowired
	private ColorMapper colorMapper;
	@RequestMapping
	public String index(Model model) {
		return "color/color.index";
	}
	
	@RequestMapping("/pageList")
	@ResponseBody
	public Map<String, Object> pageList(@SessionAttribute("accountCompanyCode") String companyCode,@RequestParam(required = false, defaultValue = "0") int start,  
			@RequestParam(required = false, defaultValue = "10") int length,@RequestParam String color) {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tc_color", color);
		params.put("tc_companycode", companyCode);
		params.put("offset", start);
		params.put("pagesize", length);
		Map<String, Object> result = colorService.pageList(params);
		return result;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public ReturnT<String> addColor(@SessionAttribute("accountCompanyCode") String companyCode,@RequestParam String color) {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tc_colorid",UUIDUtil.get32UUID());
		params.put("tc_color", color);
		params.put("tc_companycode", companyCode);
		//当前商户下颜色不能重复
		if(colorMapper.validateColor(params)>0){
			return new ReturnT<String>(500, "新增颜色失败,颜色已存在。");
		}
		int result = colorService.addColor(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "新增颜色失败！");
		}
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public ReturnT<String> updateCable(@RequestParam(required = true) String id,
			@SessionAttribute("accountCompanyCode") String companyCode,@RequestParam String color){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tc_colorid", id);
		params.put("tc_color", color);
		params.put("tc_companycode", companyCode);
		if(colorMapper.validateColor(params)>0){
			return new ReturnT<String>(500, "更新线缆信息失败,线缆规格重复。");
		}
		int result = colorService.updateCable(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "更新线缆信息失败！");
		}
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public ReturnT<String> delete(@RequestParam(required = true) String id){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tc_colorid", id);
		int result = colorService.delete(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "删除颜色失败！");
		}
	}
}
